<?php
/***************************************************************************
 *  RWCARDS - Joomla E-Cards Componment!
 *  Copyright (C) 2004/2005 by Ralf Weber
 *  Company    : LoadBrain
 *  Homepage   : www.weberr.de
 *  Version    : 2.1.0
 *  Date       : 21/12/2005
 *  License    : Released under GPL
 *
 ***************************************************************************/

// Set flag that this is a parent file
  define( '_VALID_MOS', 1 );

  $prewd = getcwd(); 			// get the current working directory
  chdir(realpath(dirname(__FILE__))); 	// change working directory to the location of this file

// Include Joomla! base-variables
  require_once( '../../configuration.php' );
  require_once( $mosConfig_absolute_path . '/globals.php' );
  require_once( $mosConfig_absolute_path . '/includes/joomla.php' );

  chdir($prewd); 			// change back to previous working dir

  if (!defined( '_RWCARDS_LANG_INCLUDED' )) 
  {
    if (file_exists( $mosConfig_absolute_path . "/components/com_rwcards/language/" . $mosConfig_lang . ".php") ) 
       {
           include_once( $mosConfig_absolute_path . "/components/com_rwcards/language/" . $mosConfig_lang . ".php");
    }
       else
       {
        include_once( $mosConfig_absolute_path . "/components/com_rwcards/language/english.php");
    }
  }

  global $database;
  $database->setQuery("SELECT * FROM #__rwcardsdata WHERE cardSent = '0' and sendDate <= '" . date("Y-m-d") . "'");
   $data_arr = $database->loadAssocList();
  $num_rows = $database->getAffectedRows();

  if ($num_rows > 0)
  {
  	$database->setQuery("SELECT id FROM #__menu WHERE published = 1 and type = 'components' and link = 'index.php?option=com_rwcards' ");
  	$itemId = $database->loadResult();

	// check the config settings
	$database->setQuery("SELECT attachement FROM #__rwcardsconfig");
	$attachement = $database->loadResult();

	$number = 0;
	while ($num_rows > $number)
	{
		$row = $data_arr[$number];
		$id = $row['id'];
		$nameTo = $row['nameTo'];
		$nameFrom = $row['nameFrom'];
		$emailTo = $row['emailTo'];
		$emailFrom = $row['emailFrom'];	   
		$picId = $row['picture'];
		$sessionId = $row['sessionId'];	   
		$message = $row['message'];

		$toSubject = _SUBJECT . $nameFrom;
		$link = $mosConfig_live_site."/index.php?option=com_rwcards&Itemid=" . $itemId . "&sessionId=" . $sessionId . "&id=" . $id . "&task=ViewCard&read=1&sendmail=1";

		/**
		* Mail function (uses phpMailer)
		* @param string From e-mail address
		* @param string From name
		* @param string/array Recipient e-mail address(es)
		* @param string E-mail subject
		* @param string Message body
		* @param boolean false = plain text, true = HTML
		* @param string/array CC e-mail address(es)
		* @param string/array BCC e-mail address(es)
		* @param string/array Attachment file name(s)
		* mosMail($from, $fromname, $recipient, $subject, $body, $mode=0, $cc=NULL, $bcc=NULL, $attachment=NULL )	
		*/
	
		// send link to card
		if ($attachement == "0")
		{
			$toMessage = _GREETING . $nameTo . "\n" . $nameFrom . _MSG_PART_1 . $link . _MSG_COPYRIGHT;
			mosMail($emailFrom, $nameFrom, $emailTo, $toSubject, $toMessage);
		}
		// send card as attachement
		else
		{
			$database->setQuery("SELECT picture FROM #__rwcards where id = '" . $picId . "'");
			$picture = $database->loadResult();
			$toMessage = _GREETING . $nameTo . "\n" . $nameFrom . _MSG_ATTACHEMENT_1 . $message . _MSG_COPYRIGHT;
			mosMail($emailFrom, $nameFrom, $emailTo, $toSubject, $toMessage, 0,  $cc=NULL, $bcc=NULL, "./images/stories/cards/" . $picture);
		}
		$database->setQuery("UPDATE #__rwcardsdata SET cardSent = '1' where id = '" . $id . "' and sessionId = '" . $sessionId . "'");
		$database->query();
		$number++;
	}

	$emailFrom = $mosConfig_mailfrom;
	$nameFrom = "RWCards Advancedate";
	$emailTo = $mosConfig_mailfrom;
	$nameTo = $mosConfig_fromname;
	$toSubject = _ADVANCEDATE_SUBJECT;
	$toMessage = _ADVANCEDATE_GREETING . $nameTo . "\n\n" . $nameFrom . ":\n" . $number . _ADVANCEDATE_MSG_PART_1  . _MSG_COPYRIGHT;

	mosMail($emailFrom, $nameFrom, $emailTo, $toSubject, $toMessage);

  }
?>
